#pragma once

#include "ftd2xx.h"
#include <vector>
#include <thread>
#include <string>
#include <functional>

#define USB_BUF_SMALLEST_SIZE 0x21
#define DATA_BUF_SIZE 16*1024


typedef std::function<void (void *param, const uint8_t* data, uint32_t len)> READ_CALLBACK;
class ftdiDevices
{
private:
    /* data */
    std::shared_ptr<std::thread> m_read_thread_ = nullptr;
    bool m_thread_start_flag_;
    bool m_start_cap_flag_;
    FT_HANDLE m_handle_ = nullptr;
    std::string m_serial_;
    std::string m_description_;
    int m_dev_index_;
    FT_DEVICE_LIST_INFO_NODE m_dev_node_;

    void* param_;
    READ_CALLBACK process_data_callBack_;
public:
    ftdiDevices();
    ~ftdiDevices();

    void SetVidPid(DWORD dwVID, DWORD dwPID);
    void SetDeviceNode(FT_DEVICE_LIST_INFO_NODE ftdiNode, int index);

    int InitDevice();

    int Start();

    int Stop();

    int StartCap();

    void addCallBack(READ_CALLBACK cb, void* param); 

private:
    bool FindFtdiDevices(std::vector<FT_DEVICE_LIST_INFO_NODE>& findDevices);
    void PrintFtdiDevices(std::vector<FT_DEVICE_LIST_INFO_NODE>& devices);
    void LoopFunction();

    int OpenDevice(int deviceNumber);
    int CloseDevice();

    int readData(uint8_t* RxBuffer);
};